package com.xiaojun.whut.Controller.Admin;

import com.alibaba.fastjson.JSON;
import com.xiaojun.whut.Entity.User;
import com.xiaojun.whut.Repository.UserRepository;
import com.xiaojun.whut.Service.PcAdminService;
import com.xiaojun.whut.annotation.MyLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.transaction.Transactional;
import java.util.HashMap;

/**
 * @author 鲁佳磊
 * @create 2020-11-02 11:00
 */
@RestController
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private UserRepository userRepository;

    @Autowired
    private PcAdminService pcAdminService;

    @MyLog("管理员进行的测试")
    @RequestMapping("/testAdmin")
    public String testAdmin() {
        return "lujialei";
    }

    @MyLog("得到所有的用户的信息，根据查询的信息，然后分页数，还有分页的大小")
    @RequestMapping("/userList")
    public String getAllUserList(@RequestParam("query") String query, @RequestParam("pageNum") int pageNum, @RequestParam("pageSize") int pageSize) {
        try {
            String allUserList = pcAdminService.getAllUserList(query, pageNum, pageSize);
            System.out.println("开始打印这个userList的信息");
            System.out.println(allUserList);
            return allUserList;
        }catch (Exception e){
            HashMap<String, Object> stringObjectHashMap = new HashMap<>();
            stringObjectHashMap.put("number", null);
            stringObjectHashMap.put("data", 0);
            return JSON.toJSONString(stringObjectHashMap);
        }
    }
    @MyLog("添加一个用户")
    @RequestMapping("/addUser")
    public String addUser(@RequestParam("userName") String username, @RequestParam("password") String password, @RequestParam("email") String email, @RequestParam("phoneNumber") String phoneNumber, @RequestParam("collage") String collage, @RequestParam("sex") String sex) {
        try {
            String s = pcAdminService.addUserService(username, password, email, phoneNumber, collage, sex);
            return s;
        }catch (Exception e){
            return "100";
        }
    }
    @MyLog("更改用户的可用状态")
    @RequestMapping("/updateUserState")
    public String updateUserState(@RequestParam("phoneNumber") String phoneNumber, @RequestParam("enable") boolean enable) {
        try {
            String s = pcAdminService.updateUserState(phoneNumber, enable);
            return s;
        } catch (Exception e) {
            return "100";
        }
    }
    @MyLog("得到一个用户根据电话号码")
    @RequestMapping("/getUserByPhoneNum")
    public String getUserByPhoneNum(@RequestParam("phoneNumber") String phoneNumber) {
        User byPhoneNumber = userRepository.findByPhoneNumber(phoneNumber);
        return JSON.toJSONString(byPhoneNumber);
    }

    @MyLog("根据id来进行删除一个用户")
    @Transactional
    @RequestMapping("/deleteUserById")
    public String deleteUserById(@RequestParam("phoneNumber") String phoneNumber) {
        try {
            String s = pcAdminService.deleteUserById(phoneNumber);
            return s;
        }catch (Exception e){
            return "100";
        }

    }
    @MyLog("编辑一个用户的信息")
    @RequestMapping("/editUserInfo")
    public String editUserInfo(@RequestParam("userName") String username, @RequestParam("password") String password, @RequestParam("email") String email, @RequestParam("phoneNumber") String phoneNumber, @RequestParam("collage") String collage, @RequestParam("sex") String sex) {
        try {

            String s = pcAdminService.editUserInfo(username, password, email, phoneNumber, collage, sex);
            return s;
        } catch (Exception e) {
            return "100";
        }
//        User byPhoneNumber = userRepository.findByPhoneNumber(phoneNumber);
//        return JSON.toJSONString(byPhoneNumber);
    }

    @MyLog("更改一个用户的权限")
    @Transactional
    @RequestMapping("/updateAuthorites")
    public String updateAuthorites(@RequestParam("phoneNumber") String phoneNumber,@RequestParam("authorities") String authorities){
        try {
            String s = pcAdminService.updateAuthorites(phoneNumber, authorities);
            return s;
        }catch (Exception e){
            return "100";
        }
    }

}
